package com.zxc.excel;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelUtil;

public class ExcelDemo {

	public static void main(String[] args) {
		
		test1();
		
//		test2();
	}

	public static void test2() {
		String req = "{\r\n" + 
				"    \"requestId\": \"489102a0dd6a4150eee01c9bcf332e44\",\r\n" + 
				"    \"tripType\": 1,\r\n" + 
				"    \"flightType\": 0,\r\n" + 
				"    \"adultNumber\": 1,\r\n" + 
				"    \"childNumber\": 0,\r\n" + 
				"    \"infantNumber\": 0,\r\n" + 
				"    \"fromDate\": \"20231123\",\r\n" + 
				"    \"retDate\": \"\",\r\n" + 
				"    \"fromCity\": \"GVA\",\r\n" + 
				"    \"toCity\": \"MAD\",\r\n" + 
				"    \"carrierCode\": [\r\n" + 
				"        \"IB\"\r\n" + 
				"    ],\r\n" + 
				"    \"includeSSR\": [],\r\n" + 
				"    \"timeout\": 199\r\n" + 
				"}";
		String result = HttpUtil.post("http://aerohubquery.fengxiangcloud.cn/aerohub4tl", req);
		System.out.println(result);
	}
	
	public static void test1() {
		File file = new File("D:\\TEST.xlsx");
		
		List<Map<String,String>> datas = new ArrayList<>();
		ExcelUtil.readBySax(file, 0, (sheetIndex,rowIndex,rowCells)->{
			if(rowIndex >= 1) {
				Map<String, String> map = new HashMap<>();
				map.put("search_id", Objects.toString(rowCells.get(0)));
				map.put("carrier", Objects.toString(rowCells.get(3)));
				map.put("fromCity", Objects.toString(rowCells.get(4)));
				map.put("toCity", Objects.toString(rowCells.get(5)));
				map.put("fromDate", Objects.toString(rowCells.get(7)));
				
				String req = "{\r\n" + 
						"    \"requestId\": \"{search_id}\",\r\n" + 
						"    \"tripType\": 1,\r\n" + 
						"    \"flightType\": 0,\r\n" + 
						"    \"adultNumber\": 1,\r\n" + 
						"    \"childNumber\": 0,\r\n" + 
						"    \"infantNumber\": 0,\r\n" + 
						"    \"fromDate\": \"{fromDate}\",\r\n" + 
						"    \"retDate\": \"\",\r\n" + 
						"    \"fromCity\": \"{fromCity}\",\r\n" + 
						"    \"toCity\": \"{toCity}\",\r\n" + 
						"    \"carrierCode\": [\r\n" + 
						"        \"{carrier}\"\r\n" + 
						"    ],\r\n" + 
						"    \"includeSSR\": [],\r\n" + 
						"    \"timeout\": 199\r\n" + 
						"}";
				req = StringUtils.replace(req, "{search_id}", map.get("search_id"));
				req = StringUtils.replace(req, "{fromDate}", map.get("fromDate"));
				req = StringUtils.replace(req, "{fromCity}", map.get("fromCity"));
				req = StringUtils.replace(req, "{toCity}", map.get("toCity"));
				req = StringUtils.replace(req, "{carrier}", map.get("carrier"));
				
				String result = HttpUtil.post("http://aerohubquery.fengxiangcloud.cn/aerohub4tl", req);
				
				result = result.length() > 1000 ? result.substring(0, 1000) : result;
				map.put("result", result);
				
				datas.add(map);
			}
		});
		
		ExcelUtil.getWriter("D:\\result.xlsx").write(datas).flush();
	}
}
